/**
 * 循环输出数组里面的数字，给定时间间隔
 * @arr Array  需要循环输出的数组，默认为 [0, 1]
 * @delay Integer 毫秒, 默认值为 1000
 */

function generateLoop({
  list = [0, 1],
  delay = 10000,
  callback = val => {
    console.log(val);
  }
} = {}) {
  let n = 0;
  function loop(arr) {
    let current = arr.shift();
    arr.push(current);    
    setTimeout(function() {
      console.log('delay:', current)
      if (callback) {
        callback(current, n);
      }
      n++;
      loop(arr);
    }, current.delay || delay);
  }
  return function() {
    loop(list);
  };
}
var loop01 = generateLoop({
  list: ["红", "绿", "蓝"],
  delay: 2000,
  callback: function(value) {
    console.log("value:", value);
  }
});
loop01();

var list = [{
  color: 'red',
  delay:  2000,  
},{
  color: 'green',
  delay:  2000,   
},{
  color: 'yellow',
  delay:  500,   
}]

var loop02 = generateLoop({
  list: list,
  callback: function(item) {
    console.log("value:", item);
  }
});
loop02();